// pages/address/address.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addList:[],
    indx:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const _this = this
    wx.request({
      url: app.globalData.url +'/area/address',
      method:'post',
      header:{
        "content-type": 'application/x-www-form-urlencoded',
        "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
        "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
      },
      success(res){
        console.log("地址列表",res)
        _this.setData({
          addList: res.data.addressList
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //点击填加地址
  addAddress(res) {
    wx.navigateTo({
      url:'/pages/addAdress/addAdress'
    })
  },
  //更改默认地址
  changeDefault(e) {
    const Oguid = e.currentTarget.dataset.guid
    const _this = this  
    wx.request({
      url: app.globalData.url +'/area/setDefault',
      method:'post',
      header:{
        "content-type": 'application/x-www-form-urlencoded',
        "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
        "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
      },
      data:{
        guid:Oguid
      },
      success(res) {
        console.log('设置默认地址',res)
        //更新默认地址选中状态
        wx.request({
          url: app.globalData.url + '/area/address',
          method: 'post',
          header: {
            "content-type": 'application/x-www-form-urlencoded',
            "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
            "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
          },
          success(res) {
            console.log("地址列表", res)
            _this.setData({
              addList: res.data.addressList
            })
          }
        })
      }
    })
  },
  //删除地址
  delAddress(e){
    const _this = this
    const Oguid = e.currentTarget.dataset.guid 
    wx.request({
      url: app.globalData.url + '/area/delAddress',
      method:'post',
      header:{
        "content-type": 'application/x-www-form-urlencoded',
        "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
        "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
      },
      data:{
        guid:Oguid
      },
      success(res) {
        console.log('删除地址',res)
        wx.request({
          url: app.globalData.url + '/area/address',
          method: 'post',
          header: {
            "content-type": 'application/x-www-form-urlencoded',
            "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
            "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
          },
          success(res) {
            console.log("地址列表", res)
            _this.setData({
              addList: res.data.addressList
            })
          }
        })
      }
    })
  },
  //编辑地址
  editAddress(e) {
    const Oguid = e.currentTarget.dataset.guid
    wx.navigateTo({
      url: '/pages/addAdress/addAdress?guid='+Oguid,
    })
  },
  //获取微信地址
  getAddress(res) {
    wx.authorize({
      scope:'scope.werun',
      success(res) {
        console.log(res)
      }
    })
    
  }
})